/**
 * 
 */
package com.lanswon.qzsmk.service.appManageService.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lanswon.qzsmk.base.AccountContants;
import com.lanswon.qzsmk.base.DwzPage;
import com.lanswon.qzsmk.base.HttpContants;
import com.lanswon.qzsmk.dao.CardProdunctDefinitionMapper;
import com.lanswon.qzsmk.dao.FeeRuleMapper;
import com.lanswon.qzsmk.dao.FeeRulesMapper;
import com.lanswon.qzsmk.model.FeeRules;
import com.lanswon.qzsmk.service.appManageService.AppFeeService;
import com.lanswon.qzsmk.service.systemManageService.ParamManageService;
import com.lanswon.qzsmk.vo.FeeRulesVo;
import com.lanswon.qzsmk.vo.ResultStatus;
import com.lanswon.qzsmk.vo.ResultVo;

/**
 * @author sun
 *
 */
@Service
public class AppFeeServiceImpl implements AppFeeService {

	@Autowired
	FeeRuleMapper frm;
	
	@Autowired
	FeeRulesMapper frsm;
	
	@Autowired
	CardProdunctDefinitionMapper cdm;
	
	@Override
	public ResultVo showAppFeeList(FeeRulesVo vo) {
		ResultVo rv = new ResultVo();
		ResultStatus rs = new ResultStatus();
		rs.setCode(HttpContants.RETURN_SUCCESS);
		rs.setMessage("返回成功");
		rv.setResultStatus(rs);
		
		List<FeeRulesVo> list = frm.queryFee(vo);
		
		for(FeeRulesVo vv:list){
			vv.setFeeTypeName(AccountContants.getFeeTypeNameByCode(vv.getFeeType()));
			vv.setIsActivate(checkIsActivate(vv.getValidStartDate(),vv.getValidEndDate()));
		}
		
		int totalCount = frm.queryFeeCount(vo);
		
		DwzPage<FeeRulesVo> dp = new DwzPage<>(vo.getCurrentPage(), vo.getNumPerPage(), totalCount);
		dp.setList(list);
		
		rv.setServiceObject(dp);
		 
		
		return rv;
	}
	
	@Override
	public String checkIsActivate(Date validStartDate,Date validEndDate ){
		Date now=new Date();
		Calendar cNow=Calendar.getInstance();
		cNow.setTime(now);
		Calendar cStart=Calendar.getInstance();
		cStart.setTime(validStartDate);
		Calendar cEnd=Calendar.getInstance();
		cEnd.setTime(validEndDate);
		if(cNow.after(cStart)&&cNow.before(cEnd)){
			return "1";
		}else {
			return "0";
		}
	}

	@Override
	public ResultVo addAppFeeRule(FeeRulesVo vo) {
		ResultVo rv = new ResultVo();
		ResultStatus rs = new ResultStatus();
		rs.setCode(HttpContants.RETURN_SUCCESS);
		//20181208 hgl 修改以下1行
		rs.setMessage("新增成功");
		rv.setResultStatus(rs);
		vo.setIsActivate("1"); 
		vo.setNow(new Date());
		if(frsm.checkExist(vo)>0){
			rs.setCode(HttpContants.RETURN_ERROR);
			rs.setMessage("规则已经存在,请勿重复添加");
			rv.setResultStatus(rs);
			return rv;
		}
		//规则
		frm.insert2(vo);
		//细则
		frsm.insert2(vo);
		
		
		
		return rv;
		
	}

	@Override
	public ResultVo modifyAppFeeRule(FeeRulesVo vo) {
		ResultVo rv = new ResultVo();
		ResultStatus rs = new ResultStatus();
		rs.setCode(HttpContants.RETURN_SUCCESS);
		//20181208 hgl 修改以下1行
		rs.setMessage("修改成功");
		rv.setResultStatus(rs);
//		vo.setNow(new Date());
//		if(frsm.checkExist(vo)>0){
//			rs.setCode(HttpContants.RETURN_ERROR);
//			rs.setMessage("规则已经存在,请勿重复添加");
//			rv.setResultStatus(rs);
//			return rv;
//		}
		//细则
		frsm.update2(vo);
		
		//规则
		frm.update2(vo);
		
		return rv;
		
		
	}

	@Override
	public FeeRulesVo queryFeeByFeeRulesId(String feeRulesId) {
		
		return frm.queryFeeByFeeRulesId(feeRulesId);
	}

	@Override
	public FeeRules queryFeeRulesByCardPCodeAndFeeType(FeeRulesVo vo) {
		if(vo.getCardpCode()==null&&vo.getCardpName()!=null)
			vo.setCardpCode(cdm.queryCardPCodeByCardProductName(vo.getCardpName()).getCardpCode());
		vo.setNow(new Date());
		FeeRules feeRules=frsm.queryFeeRulesByCardPCodeAndFeeType(vo);
		return feeRules;
	}
	
	
	
	
}
